package com.by_man.presenter.module_health_records.attention;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.by_man.presenter.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by man on 2017-02-24.
 */
public class DetailContentActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_content);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent intent = new Intent(this, AttentionContentActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

//    protected void onRestart()
//    {
//        super.onRestart();
//        Intent intent=new Intent(this,GestureLockActivity.class);
//        intent.putExtra("class",this.getClass().getName());
//        intent.putExtra("position", 0);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
//    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        // 取出数据处理
        ArrayList<HashMap<String, String>> infos = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(getIntent().getStringExtra("jsonString"));
            JSONObject temp;
            for (int j = 0; j < jsonArray.length(); j++) {
                temp = (JSONObject)jsonArray.get(j);
                HashMap<String, String> map = new HashMap<>();
                String ischeck = temp.getString("ischeck");
                if (ischeck.equals("1")) ischeck = "异常";
                else ischeck = "正常";
                map.put("ischeck", ischeck);
                map.put("reportName",temp.getString("name"));
                map.put("reportValue", temp.getString("value"));
                map.put("unit", temp.getString("unit"));
                infos.add(map);
                // 逻辑问题，不应该将其放在循环里面
            }
            SimpleAdapter adapter = new SimpleAdapter(this, infos, R.layout.detail_content_listview_item,
                    new String[]{"reportName","reportValue","unit","ischeck"},
                    new int[]{R.id.ocrkey_value,R.id.ocrvalue,R.id.unit,R.id.ischeck});
            ListView listView = (ListView) findViewById(R.id.list_view);
            listView.setAdapter(adapter);
        }catch (JSONException je) {
            je.printStackTrace();
        }
    }
}
